package xmlsync2;
/**
 * DESCRIPTION
 * Some utility functions for the Demos
 */

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import org.apache.log4j.Logger;
import datawave.util.LoggerCache;

public class xmlurl
{
	static private Logger log = LoggerCache.get(xmlurl.class.getName());

   // Helper method to create a URL from a file name
   public static URL createURL(String fileName) throws XmlsyncException
   {
      URL url = null;
      try 
      {
         url = new URL(fileName);
      } 
      catch (MalformedURLException ex) 
      {
         File f = new File(fileName);

            String path = f.getAbsolutePath();
            // This is a bunch of weird code that is required to
            // make a valid URL on the Windows platform, due
            // to inconsistencies in what getAbsolutePath returns.
            String fs = System.getProperty("file.separator");
            if (fs.length() == 1)
            {
               char sep = fs.charAt(0);
               if (sep != '/')
                  path = path.replace(sep, '/');
               if (path.charAt(0) != '/')
                  path = '/' + path;
            }
            path = "file://" + path;
         try 
         {
            url = new URL(path);
         } 
         catch (MalformedURLException e) 
         {
			Object[] args = {path, e.getMessage()};
			String msg = MessageFormat.format(Messages.getString("XMLSYNC0035"), args);
			log.error(msg);
			throw new XmlsyncException(msg);
         }
      }
      return url;
   }
}
